/* 

Replication syntax BJPolS article
"Intergenerational social mobility, political socialization, and support for the left under post-industrial realignment"
Dr. Macarena Ares, University of Barcelona
Dr. Mathilde M. van Ditmars, University of Lucerne
March 2022 (Stata version 16)

File 2/4. G-SOEP Data preparation 
*/

*German Socio-Economic Panel (SOEP), 1984-2018
*Data version used: G-SOEP data v35; DOI: 10.5684/soep.v35.
*see https://www.diw.de/sixcms/detail.php?id=diw_01.c.742256.en


*DATASET WORKING FILE CREATION*

clear all
set more off, permanently
capture log close

global data "DEFINE"
global temp "DEFINE"

*take Oesch variables from pequiv file
use pid syear e11105 using ${data}pequiv, clear // key variable is e11105 (isco-88 at 4-digit level)
rename e11105 isco_mainjob // Current occupation of respondent, coded with isco88 4-digit

*merge with needed variables from personal long file
merge 1:1 pid syear using ${data}pl, ///
	keepusing(hid plh0004 plh0011_h plh0012_h plh0013_h plb0022_h ///
	plb0057_h-plb0065 plb0067 plb0068 plc0013_h plc0014_h pli0098_v2) ///
 keep(match) nogen // only people who answered the survey

*merge with pgen variables
sort pid syear
merge 1:1 pid syear using ${data}pgen, keepusing (pgnation pgisei08 pgisei88 pgsiops08 pgsiops88 pgisced97 pgisced11 ///
pgisco08 pgisco88 pgfamstd pgegp88 pgegp08) keep (match) nogen

*merge with ppfad variables 
sort pid syear
merge 1:1 pid syear using ${data}ppathl, keepusing (loc1989 sex gebjahr) keep (match) nogen
sort pid syear
save ${temp}longfile_temp, replace

*merge with household variables // check if it's hid or hhnr that is used in these respective files
sort hid syear
merge m:1 hid syear using ${data}hl, keepusing (hlc0005_h hlc0043) keep (match) nogen
sort pid syear
save SOEP_v35_workfile, replace



********************************************************************************
*** PREPARATORY CODING**********************************************************


use SOEP_v35_workfile, clear

set more off
label language EN



*Dofile Daniel Oesch [adapted for plb0057_v1/v2/v3, 63, 64, this used to be 1 var]

**** Recode occupation variable (isco88 com 4-digit) for respondents
tab isco_mainjob
recode isco_mainjob (66666 77777 88888 99999=-9), copyrest
label variable isco_mainjob "Current occupation of respondent - isco88 4-digit"
tab isco_mainjob

*** Recode employment status for respondents (I)

gen emplrel_r=1 if plb0058>0 | plb0063_v1>0 | plb0063_v2>0 | plb0063_v3>0 | ///
plb0064_v1>0 | plb0064_v2>0 | plb0064_v3>0 | plb0065>0 //Workers, apprentices, employees, public servants
replace emplrel_r=2 if (plb0057_v1>0 | plb0057_v2>0 | plb0057_v3>0)  & (plb0057_v1<5 | plb0057_v2!=3 | plb0057_v3<6) // Self-employed
replace emplrel_r=3 if (plb0057_v1==5 | plb0057_v2==3 | plb0057_v3==6) // Working family member

gen emplno_r=1 if plb0059==2 | plb0060==2 | plb0061==2 // 1-9 employees
replace emplno_r=2 if plb0059==3 | plb0060==3 | plb0061==3 // 10+ employees
replace emplno_r=0 if emplno==. & emplrel_r!=.

recode emplrel_r emplno_r (.=9)


**** Recode employment status for respondents (II)

tab emplrel
label define emplrel_r ///
1 "Employee" ///
2 "Self-employed" ///
3 "Working for own family business" ///
9 "Missing"
label value emplrel_r emplrel_r
tab emplrel_r

tab emplno
label define emplno_r ///
0 "0 employees" ///
1 "1-9 employees" ///
2 "10+ employees"
label value emplno_r emplno_r
tab emplno_r

gen selfem_mainjob=.
replace selfem_mainjob=1 if emplrel_r==1 | emplrel_r==9
replace selfem_mainjob=2 if emplrel_r==2 & emplno_r==0
replace selfem_mainjob=2 if emplrel_r==3
replace selfem_mainjob=3 if emplrel_r==2 & emplno_r==1
replace selfem_mainjob=4 if emplrel_r==2 & emplno_r==2
label variable selfem_mainjob "Employment status for respondants"
label define selfem_mainjob ///
1 "Not self-employed" ///
2 "Self-empl without employees" ///
3 "Self-empl with 1-9 employees" ///
4 "Self-empl with 10 or more"
label value selfem_mainjob selfem_mainjob
tab selfem_mainjob


*************************************************
* Create Oesch class schema for respondents
*************************************************

gen class16_r = -9

* Large employers (1)

replace class16_r=1 if selfem_mainjob==4


* Self-employed professionals (2)

replace class16_r=2 if (selfem_mainjob==2 | selfem_mainjob==3) & (isco_mainjob >= 2000 & isco_mainjob <= 2229) 
replace class16_r=2 if (selfem_mainjob==2 | selfem_mainjob==3) & (isco_mainjob >= 2300 & isco_mainjob <= 2470)

* Small business owners with employees (3)

replace class16_r=3 if (selfem_mainjob==3) & (isco_mainjob >= 1000 & isco_mainjob <= 1999)
replace class16_r=3 if (selfem_mainjob==3) & (isco_mainjob >= 3000 & isco_mainjob <= 9333)
replace class16_r=3 if (selfem_mainjob==3) & (isco_mainjob == 2230)

* Small business owners without employees (4)

replace class16_r=4 if (selfem_mainjob==2) & (isco_mainjob >= 1000 & isco_mainjob <= 1999)
replace class16_r=4 if (selfem_mainjob==2) & (isco_mainjob >= 3000 & isco_mainjob <= 9333)
replace class16_r=4 if (selfem_mainjob==2) & (isco_mainjob == 2230)

* Technical experts (5)

replace class16_r=5 if (selfem_mainjob==1) & (isco_mainjob >= 2100 & isco_mainjob <= 2213)

* Technicians (6)

replace class16_r=6 if (selfem_mainjob==1) & (isco_mainjob >= 3100 & isco_mainjob <= 3152)
replace class16_r=6 if (selfem_mainjob==1) & (isco_mainjob >= 3210 & isco_mainjob <= 3213)
replace class16_r=6 if (selfem_mainjob==1) & (isco_mainjob == 3434)

* Skilled craft and production workers (7)

replace class16_r=7 if (selfem_mainjob==1) & (isco_mainjob >= 6000 & isco_mainjob <= 7442)
replace class16_r=7 if (selfem_mainjob==1) & (isco_mainjob >= 8310 & isco_mainjob <= 8312)
replace class16_r=7 if (selfem_mainjob==1) & (isco_mainjob >= 8324 & isco_mainjob <= 8330)
replace class16_r=7 if (selfem_mainjob==1) & (isco_mainjob >= 8332 & isco_mainjob <= 8340)

* Low-skilled production workers (8)

replace class16_r=8 if (selfem_mainjob==1) & (isco_mainjob >= 8000 & isco_mainjob <= 8300)
replace class16_r=8 if (selfem_mainjob==1) & (isco_mainjob >= 8320 & isco_mainjob <= 8321)
replace class16_r=8 if (selfem_mainjob==1) & (isco_mainjob == 8331)
replace class16_r=8 if (selfem_mainjob==1) & (isco_mainjob >= 9153 & isco_mainjob <= 9333)

* Higher-grade managers and administrators (9)

replace class16_r=9 if (selfem_mainjob==1) & (isco_mainjob >= 1000 & isco_mainjob <= 1239)
replace class16_r=9 if (selfem_mainjob==1) & (isco_mainjob >= 2400 & isco_mainjob <= 2429)
replace class16_r=9 if (selfem_mainjob==1) & (isco_mainjob == 2441)
replace class16_r=9 if (selfem_mainjob==1) & (isco_mainjob == 2470)

* Lower-grade managers and administrators (10)

replace class16_r=10 if (selfem_mainjob==1) & (isco_mainjob >= 1300 & isco_mainjob <= 1319)
replace class16_r=10 if (selfem_mainjob==1) & (isco_mainjob >= 3400 & isco_mainjob <= 3433)
replace class16_r=10 if (selfem_mainjob==1) & (isco_mainjob >= 3440 & isco_mainjob <= 3450)

* Skilled clerks (11)

replace class16_r=11 if (selfem_mainjob==1) & (isco_mainjob >= 4000 & isco_mainjob <= 4112)
replace class16_r=11 if (selfem_mainjob==1) & (isco_mainjob >= 4114 & isco_mainjob <= 4210)
replace class16_r=11 if (selfem_mainjob==1) & (isco_mainjob >= 4212 & isco_mainjob <= 4222)

* Low-skilled clerks (12)

replace class16_r=12 if (selfem_mainjob==1) & (isco_mainjob == 4113)
replace class16_r=12 if (selfem_mainjob==1) & (isco_mainjob == 4211)
replace class16_r=12 if (selfem_mainjob==1) & (isco_mainjob == 4223)

* Socio-cultural professionals (13)

replace class16_r=13 if (selfem_mainjob==1) & (isco_mainjob >= 2220 &  isco_mainjob <= 2229)
replace class16_r=13 if (selfem_mainjob==1) & (isco_mainjob >= 2300 &  isco_mainjob <= 2320)
replace class16_r=13 if (selfem_mainjob==1) & (isco_mainjob >= 2340 &  isco_mainjob <= 2359)
replace class16_r=13 if (selfem_mainjob==1) & (isco_mainjob >= 2430 &  isco_mainjob <= 2440)
replace class16_r=13 if (selfem_mainjob==1) & (isco_mainjob >= 2442 &  isco_mainjob <= 2443)
replace class16_r=13 if (selfem_mainjob==1) & (isco_mainjob == 2445)
replace class16_r=13 if (selfem_mainjob==1) & (isco_mainjob == 2451)
replace class16_r=13 if (selfem_mainjob==1) & (isco_mainjob == 2460)

* Socio-cultural semi-professionals (14)

replace class16_r=14 if (selfem_mainjob==1) & (isco_mainjob == 2230)
replace class16_r=14 if (selfem_mainjob==1) & (isco_mainjob >= 2330 & isco_mainjob <= 2332)
replace class16_r=14 if (selfem_mainjob==1) & (isco_mainjob == 2444)
replace class16_r=14 if (selfem_mainjob==1) & (isco_mainjob >= 2446 & isco_mainjob <= 2450)
replace class16_r=14 if (selfem_mainjob==1) & (isco_mainjob >= 2452 & isco_mainjob <= 2455)
replace class16_r=14 if (selfem_mainjob==1) & (isco_mainjob == 3200)
replace class16_r=14 if (selfem_mainjob==1) & (isco_mainjob >= 3220 & isco_mainjob <= 3224)
replace class16_r=14 if (selfem_mainjob==1) & (isco_mainjob == 3226)
replace class16_r=14 if (selfem_mainjob==1) & (isco_mainjob >= 3229 & isco_mainjob <= 3340)
replace class16_r=14 if (selfem_mainjob==1) & (isco_mainjob >= 3460 & isco_mainjob <= 3472)
replace class16_r=14 if (selfem_mainjob==1) & (isco_mainjob == 3480)

* Skilled service (15)

replace class16_r=15 if (selfem_mainjob==1) & (isco_mainjob == 3225)
replace class16_r=15 if (selfem_mainjob==1) & (isco_mainjob >= 3227 & isco_mainjob <= 3228)
replace class16_r=15 if (selfem_mainjob==1) & (isco_mainjob >= 3473 & isco_mainjob <= 3475)
replace class16_r=15 if (selfem_mainjob==1) & (isco_mainjob >= 5000 & isco_mainjob <= 5113)
replace class16_r=15 if (selfem_mainjob==1) & (isco_mainjob == 5122)
replace class16_r=15 if (selfem_mainjob==1) & (isco_mainjob >= 5131 & isco_mainjob <= 5132)
replace class16_r=15 if (selfem_mainjob==1) & (isco_mainjob >= 5140 & isco_mainjob <= 5141)
replace class16_r=15 if (selfem_mainjob==1) & (isco_mainjob == 5143)
replace class16_r=15 if (selfem_mainjob==1) & (isco_mainjob >= 5160 & isco_mainjob <= 5220)
replace class16_r=15 if (selfem_mainjob==1) & (isco_mainjob == 8323)

* Low-skilled service (16)

replace class16_r=16 if (selfem_mainjob==1) & (isco_mainjob >= 5120 & isco_mainjob <= 5121)
replace class16_r=16 if (selfem_mainjob==1) & (isco_mainjob >= 5123 & isco_mainjob <= 5130)
replace class16_r=16 if (selfem_mainjob==1) & (isco_mainjob >= 5133 & isco_mainjob <= 5139)
replace class16_r=16 if (selfem_mainjob==1) & (isco_mainjob == 5142)
replace class16_r=16 if (selfem_mainjob==1) & (isco_mainjob == 5149)
replace class16_r=16 if (selfem_mainjob==1) & (isco_mainjob == 5230)
replace class16_r=16 if (selfem_mainjob==1) & (isco_mainjob == 8322)
replace class16_r=16 if (selfem_mainjob==1) & (isco_mainjob >= 9100 &  isco_mainjob <= 9152)

mvdecode class16_r, mv(-9)
label variable class16_r "Respondent's Oesch class position - 16 classes"
label define class16_r ///
1 "Large employers" ///
2 "Self-employed professionals" ///
3 "Small business owners with employees" ///
4 "Small business owners without employees" ///
5 "Technical experts" ///
6 "Technicians" ///
7 "Skilled craft and production workers" ///
8 "Low-skilled craft and production workers" ///
9 "Higher-grade managers and administrators" ///
10 "Lower-grade managers and administrators" ///
11 "Skilled clerks" ///
12 "Low-skilled clerks" ///
13 "Socio-cultural professionals" ///
14 "Socio-cultural semi-professionals" ///
15 "Skilled service" ///
16 "Low-skilled service"
label value class16_r class16_r
tab class16_r

recode class16_r (1 2=1)(3 4=2)(5 6=3)(7 8=4)(9 10=5)(11 12=6)(13 14=7)(15 16=8), gen(class8_r)
label variable class8_r "Respondent's Oesch class position - 8 classes"
label define class8_r ///
1 "Self-employed professionals and large employers" ///
2 "Small business owners" ///
3 "Technical (semi-)professionals" ///
4 "Production workers" ///
5 "(Associate) managers" ///
6 "Clerks" ///
7 "Socio-cultural (semi-)professionals" ///
8 "Service workers"
label value class8_r class8_r
tab class8_r

recode class16_r (1 2 5 9 13=1)(6 10 14=2)(3 4=3)(7 11 15=4)(8 12 16=5), gen(class5_r)
label variable class5_r "Respondent's Oesch class position - 5 classes"
label define  class5_r ///
1 "Higher-grade service class" ///
2 "Lower-grade service class" ///
3 "Small business owners" ///
4 "Skilled workers" ///
5 "Low-skilled workers"
label value class5_r class5_r
tab class5_r


global class3noemp class5_r==3 & emplno_r==0 // Oesch class 3 and 0 employee
gen class4=class5_r        // recoding to 4 classes with the following change: class5_r==3
replace class4=2 if class5_r==3 & emplno_r==1 // 1-9 employees
replace class4=1 if $class3noemp & ((isco_>1000 & isco_<1300) | (isco_>2000 & isco_<3000))
replace class4=2 if $class3noemp & ((isco_>=1300 & isco_<2000) | isco_==2331 | isco_==2446 ///
| (isco_>=3000 & isco_<4000))
replace class4=4 if $class3noemp & (isco_==3228 | isco_==3475 | isco_>=4111)
replace class4=5 if $class3noemp & (isco_==4113 | isco_==4211 | isco_==4223 | isco_==5121 ///
| isco_==5123 | isco_==5133 | isco_==5139 | isco_==5149 | (isco_>8000 & isco_< 8300) | isco_==8322 ///
| isco_==8331 | (isco_>=9131 & isco_<.))
recode class4 (4=3) (5=4)            // 
label define class4_r ///
1 "Upper-middle class" ///
2 "Lower-middle class" ///
3 "Skilled workers" ///
4 "Low-skilled workers"
label value class4 class4_r
tab class4
** end of dofile Oesch

*Recode fprofstat and fisco88 into Oesch' class scheme, for father

gen class16_v = -9

* Large employers (1)

replace class16_v=1 if fprofstat==413 | fprofstat==423 | fprofstat==433


* Self-employed professionals (2)

replace class16_v=2 if (fprofstat==400 | fprofstat==420 | fprofstat==421 | fprofstat==422 | fprofstat==424 ) & (fisco88 >= 2000 & fisco88 <= 2229) 
replace class16_v=2 if (fprofstat==400 | fprofstat==420 | fprofstat==421 | fprofstat==422 | fprofstat==424) & (fisco88 >= 2300 & fisco88 <= 2470)

replace class16_v=2 if (fprofstat==430 | fprofstat==431 | fprofstat==432 | fprofstat==434 | fprofstat==440) & (fisco88 >= 2000 & fisco88 <= 2229) 
replace class16_v=2 if (fprofstat==430 | fprofstat==431 | fprofstat==432 | fprofstat==434 | fprofstat==440) & (fisco88 >= 2300 & fisco88 <= 2470)


* Small business owners with employees (3)

replace class16_v=3 if (fprofstat==412 | fprofstat==422 | fprofstat==432) & (fisco88 >= 1000 & fisco88 <= 1999)
replace class16_v=3 if (fprofstat==412 | fprofstat==422 | fprofstat==432) & (fisco88 >= 3000 & fisco88 <= 9333)
replace class16_v=3 if (fprofstat==412 | fprofstat==422 | fprofstat==432) & (fisco88 == 2230)

* Small business owners without employees (4)

replace class16_v=4 if (fprofstat==400 | fprofstat==410 | fprofstat==411 | fprofstat==420 | fprofstat==421 | fprofstat==431 | fprofstat==440) & (fisco88 >= 1000 & fisco88 <= 1999)
replace class16_v=4 if (fprofstat==400 | fprofstat==410 | fprofstat==411 | fprofstat==420 | fprofstat==421 | fprofstat==431 | fprofstat==440) & (fisco88 >= 3000 & fisco88 <= 9333)
replace class16_v=4 if (fprofstat==400 | fprofstat==410 | fprofstat==411 | fprofstat==420 | fprofstat==421 | fprofstat==431 | fprofstat==440) & (fisco88 == 2230)

* Technical experts (5)

replace class16_v=5 if (fprofstat<400 | fprofstat>440) & (fisco88 >= 2100 & fisco88 <= 2213)

* Technicians (6)

replace class16_v=6 if (fprofstat<400 | fprofstat>440) & (fisco88 >= 3100 & fisco88 <= 3152)
replace class16_v=6 if (fprofstat<400 | fprofstat>440) & (fisco88 >= 3210 & fisco88 <= 3213)
replace class16_v=6 if (fprofstat<400 | fprofstat>440) & (fisco88 == 3434)

* Skilled craft and production workers (7)

replace class16_v=7 if (fprofstat<400 | fprofstat>440) & (fisco88 >= 6000 & fisco88 <= 7442)
replace class16_v=7 if (fprofstat<400 | fprofstat>440) & (fisco88 >= 8310 & fisco88 <= 8312)
replace class16_v=7 if (fprofstat<400 | fprofstat>440) & (fisco88 >= 8324 & fisco88 <= 8330)
replace class16_v=7 if (fprofstat<400 | fprofstat>440) & (fisco88 >= 8332 & fisco88 <= 8340)

* Low-skilled production workers (8)

replace class16_v=8 if (fprofstat<400 | fprofstat>440) & (fisco88 >= 8000 & fisco88 <= 8300)
replace class16_v=8 if (fprofstat<400 | fprofstat>440) & (fisco88 >= 8320 & fisco88 <= 8321)
replace class16_v=8 if (fprofstat<400 | fprofstat>440) & (fisco88 == 8331)
replace class16_v=8 if (fprofstat<400 | fprofstat>440) & (fisco88 >= 9153 & fisco88 <= 9333)

* Higher-grade managers and administrators (9)

replace class16_v=9 if (fprofstat<400 | fprofstat>440) & (fisco88 >= 1000 & fisco88 <= 1239)
replace class16_v=9 if (fprofstat<400 | fprofstat>440) & (fisco88 >= 2400 & fisco88 <= 2429)
replace class16_v=9 if (fprofstat<400 | fprofstat>440) & (fisco88 == 2441)
replace class16_v=9 if (fprofstat<400 | fprofstat>440) & (fisco88 == 2470)

* Lower-grade managers and administrators (10)

replace class16_v=10 if (fprofstat<400 | fprofstat>440) & (fisco88 >= 1300 & fisco88 <= 1319)
replace class16_v=10 if (fprofstat<400 | fprofstat>440) & (fisco88 >= 3400 & fisco88 <= 3433)
replace class16_v=10 if (fprofstat<400 | fprofstat>440) & (fisco88 >= 3440 & fisco88 <= 3450)

* Skilled clerks (11)

replace class16_v=11 if (fprofstat<400 | fprofstat>440) & (fisco88 >= 4000 & fisco88 <= 4112)
replace class16_v=11 if (fprofstat<400 | fprofstat>440) & (fisco88 >= 4114 & fisco88 <= 4210)
replace class16_v=11 if (fprofstat<400 | fprofstat>440) & (fisco88 >= 4212 & fisco88 <= 4222)

* Low-skilled clerks (12)

replace class16_v=12 if (fprofstat<400 | fprofstat>440) & (fisco88 == 4113)
replace class16_v=12 if (fprofstat<400 | fprofstat>440) & (fisco88 == 4211)
replace class16_v=12 if (fprofstat<400 | fprofstat>440) & (fisco88 == 4223)

* Socio-cultural professionals (13)

replace class16_v=13 if (fprofstat<400 | fprofstat>440) & (fisco88 >= 2220 &  fisco88 <= 2229)
replace class16_v=13 if (fprofstat<400 | fprofstat>440) & (fisco88 >= 2300 &  fisco88 <= 2320)
replace class16_v=13 if (fprofstat<400 | fprofstat>440) & (fisco88 >= 2340 &  fisco88 <= 2359)
replace class16_v=13 if (fprofstat<400 | fprofstat>440) & (fisco88 >= 2430 &  fisco88 <= 2440)
replace class16_v=13 if (fprofstat<400 | fprofstat>440) & (fisco88 >= 2442 &  fisco88 <= 2443)
replace class16_v=13 if (fprofstat<400 | fprofstat>440) & (fisco88 == 2445)
replace class16_v=13 if (fprofstat<400 | fprofstat>440) & (fisco88 == 2451)
replace class16_v=13 if (fprofstat<400 | fprofstat>440) & (fisco88 == 2460)

* Socio-cultural semi-professionals (14)

replace class16_v=14 if (fprofstat<400 | fprofstat>440) & (fisco88 == 2230)
replace class16_v=14 if (fprofstat<400 | fprofstat>440) & (fisco88 >= 2330 & fisco88 <= 2332)
replace class16_v=14 if (fprofstat<400 | fprofstat>440) & (fisco88 == 2444)
replace class16_v=14 if (fprofstat<400 | fprofstat>440) & (fisco88 >= 2446 & fisco88 <= 2450)
replace class16_v=14 if (fprofstat<400 | fprofstat>440) & (fisco88 >= 2452 & fisco88 <= 2455)
replace class16_v=14 if (fprofstat<400 | fprofstat>440) & (fisco88 == 3200)
replace class16_v=14 if (fprofstat<400 | fprofstat>440) & (fisco88 >= 3220 & fisco88 <= 3224)
replace class16_v=14 if (fprofstat<400 | fprofstat>440) & (fisco88 == 3226)
replace class16_v=14 if (fprofstat<400 | fprofstat>440) & (fisco88 >= 3229 & fisco88 <= 3340)
replace class16_v=14 if (fprofstat<400 | fprofstat>440) & (fisco88 >= 3460 & fisco88 <= 3472)
replace class16_v=14 if (fprofstat<400 | fprofstat>440) & (fisco88 == 3480)

* Skilled service (15)

replace class16_v=15 if (fprofstat<400 | fprofstat>440) & (fisco88 == 3225)
replace class16_v=15 if (fprofstat<400 | fprofstat>440) & (fisco88 >= 3227 & fisco88 <= 3228)
replace class16_v=15 if (fprofstat<400 | fprofstat>440) & (fisco88 >= 3473 & fisco88 <= 3475)
replace class16_v=15 if (fprofstat<400 | fprofstat>440) & (fisco88 >= 5000 & fisco88 <= 5113)
replace class16_v=15 if (fprofstat<400 | fprofstat>440) & (fisco88 == 5122)
replace class16_v=15 if (fprofstat<400 | fprofstat>440) & (fisco88 >= 5131 & fisco88 <= 5132)
replace class16_v=15 if (fprofstat<400 | fprofstat>440) & (fisco88 >= 5140 & fisco88 <= 5141)
replace class16_v=15 if (fprofstat<400 | fprofstat>440) & (fisco88 == 5143)
replace class16_v=15 if (fprofstat<400 | fprofstat>440) & (fisco88 >= 5160 & fisco88 <= 5220)
replace class16_v=15 if (fprofstat<400 | fprofstat>440) & (fisco88 == 8323)

* Low-skilled service (16)

replace class16_v=16 if (fprofstat<400 | fprofstat>440) & (fisco88 >= 5120 & fisco88 <= 5121)
replace class16_v=16 if (fprofstat<400 | fprofstat>440) & (fisco88 >= 5123 & fisco88 <= 5130)
replace class16_v=16 if (fprofstat<400 | fprofstat>440) & (fisco88 >= 5133 & fisco88 <= 5139)
replace class16_v=16 if (fprofstat<400 | fprofstat>440) & (fisco88 == 5142)
replace class16_v=16 if (fprofstat<400 | fprofstat>440) & (fisco88 == 5149)
replace class16_v=16 if (fprofstat<400 | fprofstat>440) & (fisco88 == 5230)
replace class16_v=16 if (fprofstat<400 | fprofstat>440) & (fisco88 == 8322)
replace class16_v=16 if (fprofstat<400 | fprofstat>440) & (fisco88 >= 9100 &  fisco88 <= 9152)

mvdecode class16_v, mv(-9)
label variable class16_v "Father's Oesch class position - 16 classes"
label value class16_v class16_r
tab class16_v

recode class16_v (1 2=1)(3 4=2)(5 6=3)(7 8=4)(9 10=5)(11 12=6)(13 14=7)(15 16=8), gen(class8_v)
label variable class8_v "Father's Oesch class position - 8 classes"
label value class8_v class8_r
tab class8_v

recode class16_v (1 2 5 9 13=1)(6 10 14=2)(3 4=3)(7 11 15=4)(8 12 16=5), gen(class5_v)
label variable class5_v "Father's Oesch class position - 5 classes"
label value class5_v class5_r
tab class5_v


********************************************
*Recode mother's class location Oesch scheme

*Recode mprofstat and misco88 into Oesch' class scheme, for mother

gen class16_m = -9

* Large employers (1)

replace class16_m=1 if mprofstat==413 | mprofstat==423 | mprofstat==433


* Self-employed professionals (2)

replace class16_m=2 if (mprofstat==400 | mprofstat==420 | mprofstat==421 | mprofstat==422 | mprofstat==424 ) & (misco88 >= 2000 & misco88 <= 2229) 
replace class16_m=2 if (mprofstat==400 | mprofstat==420 | mprofstat==421 | mprofstat==422 | mprofstat==424) & (misco88 >= 2300 & misco88 <= 2470)

replace class16_m=2 if (mprofstat==430 | mprofstat==431 | mprofstat==432 | mprofstat==434 | mprofstat==440) & (misco88 >= 2000 & misco88 <= 2229) 
replace class16_m=2 if (mprofstat==430 | mprofstat==431 | mprofstat==432 | mprofstat==434 | mprofstat==440) & (misco88 >= 2300 & misco88 <= 2470)


* Small business owners with employees (3)

replace class16_m=3 if (mprofstat==412 | mprofstat==422 | mprofstat==432) & (misco88 >= 1000 & misco88 <= 1999)
replace class16_m=3 if (mprofstat==412 | mprofstat==422 | mprofstat==432) & (misco88 >= 3000 & misco88 <= 9333)
replace class16_m=3 if (mprofstat==412 | mprofstat==422 | mprofstat==432) & (misco88 == 2230)

* Small business owners without employees (4)

replace class16_m=4 if (mprofstat==400 | mprofstat==410 | mprofstat==411 | mprofstat==420 | mprofstat==421 | mprofstat==431 | mprofstat==440) & (misco88 >= 1000 & misco88 <= 1999)
replace class16_m=4 if (mprofstat==400 | mprofstat==410 | mprofstat==411 | mprofstat==420 | mprofstat==421 | mprofstat==431 | mprofstat==440) & (misco88 >= 3000 & misco88 <= 9333)
replace class16_m=4 if (mprofstat==400 | mprofstat==410 | mprofstat==411 | mprofstat==420 | mprofstat==421 | mprofstat==431 | mprofstat==440) & (misco88 == 2230)

* Technical experts (5)

replace class16_m=5 if (mprofstat<400 | mprofstat>440) & (misco88 >= 2100 & misco88 <= 2213)

* Technicians (6)

replace class16_m=6 if (mprofstat<400 | mprofstat>440) & (misco88 >= 3100 & misco88 <= 3152)
replace class16_m=6 if (mprofstat<400 | mprofstat>440) & (misco88 >= 3210 & misco88 <= 3213)
replace class16_m=6 if (mprofstat<400 | mprofstat>440) & (misco88 == 3434)

* Skilled craft and production workers (7)

replace class16_m=7 if (mprofstat<400 | mprofstat>440) & (misco88 >= 6000 & misco88 <= 7442)
replace class16_m=7 if (mprofstat<400 | mprofstat>440) & (misco88 >= 8310 & misco88 <= 8312)
replace class16_m=7 if (mprofstat<400 | mprofstat>440) & (misco88 >= 8324 & misco88 <= 8330)
replace class16_m=7 if (mprofstat<400 | mprofstat>440) & (misco88 >= 8332 & misco88 <= 8340)

* Low-skilled production workers (8)

replace class16_m=8 if (mprofstat<400 | mprofstat>440) & (misco88 >= 8000 & misco88 <= 8300)
replace class16_m=8 if (mprofstat<400 | mprofstat>440) & (misco88 >= 8320 & misco88 <= 8321)
replace class16_m=8 if (mprofstat<400 | mprofstat>440) & (misco88 == 8331)
replace class16_m=8 if (mprofstat<400 | mprofstat>440) & (misco88 >= 9153 & misco88 <= 9333)

* Higher-grade managers and administrators (9)

replace class16_m=9 if (mprofstat<400 | mprofstat>440) & (misco88 >= 1000 & misco88 <= 1239)
replace class16_m=9 if (mprofstat<400 | mprofstat>440) & (misco88 >= 2400 & misco88 <= 2429)
replace class16_m=9 if (mprofstat<400 | mprofstat>440) & (misco88 == 2441)
replace class16_m=9 if (mprofstat<400 | mprofstat>440) & (misco88 == 2470)

* Lower-grade managers and administrators (10)

replace class16_m=10 if (mprofstat<400 | mprofstat>440) & (misco88 >= 1300 & misco88 <= 1319)
replace class16_m=10 if (mprofstat<400 | mprofstat>440) & (misco88 >= 3400 & misco88 <= 3433)
replace class16_m=10 if (mprofstat<400 | mprofstat>440) & (misco88 >= 3440 & misco88 <= 3450)

* Skilled clerks (11)

replace class16_m=11 if (mprofstat<400 | mprofstat>440) & (misco88 >= 4000 & misco88 <= 4112)
replace class16_m=11 if (mprofstat<400 | mprofstat>440) & (misco88 >= 4114 & misco88 <= 4210)
replace class16_m=11 if (mprofstat<400 | mprofstat>440) & (misco88 >= 4212 & misco88 <= 4222)

* Low-skilled clerks (12)

replace class16_m=12 if (mprofstat<400 | mprofstat>440) & (misco88 == 4113)
replace class16_m=12 if (mprofstat<400 | mprofstat>440) & (misco88 == 4211)
replace class16_m=12 if (mprofstat<400 | mprofstat>440) & (misco88 == 4223)

* Socio-cultural professionals (13)

replace class16_m=13 if (mprofstat<400 | mprofstat>440) & (misco88 >= 2220 &  misco88 <= 2229)
replace class16_m=13 if (mprofstat<400 | mprofstat>440) & (misco88 >= 2300 &  misco88 <= 2320)
replace class16_m=13 if (mprofstat<400 | mprofstat>440) & (misco88 >= 2340 &  misco88 <= 2359)
replace class16_m=13 if (mprofstat<400 | mprofstat>440) & (misco88 >= 2430 &  misco88 <= 2440)
replace class16_m=13 if (mprofstat<400 | mprofstat>440) & (misco88 >= 2442 &  misco88 <= 2443)
replace class16_m=13 if (mprofstat<400 | mprofstat>440) & (misco88 == 2445)
replace class16_m=13 if (mprofstat<400 | mprofstat>440) & (misco88 == 2451)
replace class16_m=13 if (mprofstat<400 | mprofstat>440) & (misco88 == 2460)

* Socio-cultural semi-professionals (14)

replace class16_m=14 if (mprofstat<400 | mprofstat>440) & (misco88 == 2230)
replace class16_m=14 if (mprofstat<400 | mprofstat>440) & (misco88 >= 2330 & misco88 <= 2332)
replace class16_m=14 if (mprofstat<400 | mprofstat>440) & (misco88 == 2444)
replace class16_m=14 if (mprofstat<400 | mprofstat>440) & (misco88 >= 2446 & misco88 <= 2450)
replace class16_m=14 if (mprofstat<400 | mprofstat>440) & (misco88 >= 2452 & misco88 <= 2455)
replace class16_m=14 if (mprofstat<400 | mprofstat>440) & (misco88 == 3200)
replace class16_m=14 if (mprofstat<400 | mprofstat>440) & (misco88 >= 3220 & misco88 <= 3224)
replace class16_m=14 if (mprofstat<400 | mprofstat>440) & (misco88 == 3226)
replace class16_m=14 if (mprofstat<400 | mprofstat>440) & (misco88 >= 3229 & misco88 <= 3340)
replace class16_m=14 if (mprofstat<400 | mprofstat>440) & (misco88 >= 3460 & misco88 <= 3472)
replace class16_m=14 if (mprofstat<400 | mprofstat>440) & (misco88 == 3480)

* Skilled service (15)

replace class16_m=15 if (mprofstat<400 | mprofstat>440) & (misco88 == 3225)
replace class16_m=15 if (mprofstat<400 | mprofstat>440) & (misco88 >= 3227 & misco88 <= 3228)
replace class16_m=15 if (mprofstat<400 | mprofstat>440) & (misco88 >= 3473 & misco88 <= 3475)
replace class16_m=15 if (mprofstat<400 | mprofstat>440) & (misco88 >= 5000 & misco88 <= 5113)
replace class16_m=15 if (mprofstat<400 | mprofstat>440) & (misco88 == 5122)
replace class16_m=15 if (mprofstat<400 | mprofstat>440) & (misco88 >= 5131 & misco88 <= 5132)
replace class16_m=15 if (mprofstat<400 | mprofstat>440) & (misco88 >= 5140 & misco88 <= 5141)
replace class16_m=15 if (mprofstat<400 | mprofstat>440) & (misco88 == 5143)
replace class16_m=15 if (mprofstat<400 | mprofstat>440) & (misco88 >= 5160 & misco88 <= 5220)
replace class16_m=15 if (mprofstat<400 | mprofstat>440) & (misco88 == 8323)

* Low-skilled service (16)

replace class16_m=16 if (mprofstat<400 | mprofstat>440) & (misco88 >= 5120 & misco88 <= 5121)
replace class16_m=16 if (mprofstat<400 | mprofstat>440) & (misco88 >= 5123 & misco88 <= 5130)
replace class16_m=16 if (mprofstat<400 | mprofstat>440) & (misco88 >= 5133 & misco88 <= 5139)
replace class16_m=16 if (mprofstat<400 | mprofstat>440) & (misco88 == 5142)
replace class16_m=16 if (mprofstat<400 | mprofstat>440) & (misco88 == 5149)
replace class16_m=16 if (mprofstat<400 | mprofstat>440) & (misco88 == 5230)
replace class16_m=16 if (mprofstat<400 | mprofstat>440) & (misco88 == 8322)
replace class16_m=16 if (mprofstat<400 | mprofstat>440) & (misco88 >= 9100 &  misco88 <= 9152)

mvdecode class16_m, mv(-9)
label variable class16_m "Mother's Oesch class position - 16 classes"
label value class16_m class16_r
tab class16_m

recode class16_m (1 2=1)(3 4=2)(5 6=3)(7 8=4)(9 10=5)(11 12=6)(13 14=7)(15 16=8), gen(class8_m)
label variable class8_m "Mother's Oesch class position - 8 classes"
label value class8_m class8_r
tab class8_m

recode class16_m (1 2 5 9 13=1)(6 10 14=2)(3 4=3)(7 11 15=4)(8 12 16=5), gen(class5_m)
label variable class5_m "Mother's Oesch class position - 5 classes"
label value class5_m class5_r
tab class5_m

***********************
*combine into parental class

tab1 class8_v class8_m

capture drop class8_parent
gen class8_parent = class8_v
replace class8_parent = class8_m if class8_v==.
label values class8_parent class8_r

tab1 class8_v class8_m class8_parent


* Simplified measure of parental class
recode class8_parent (3=1) (5=1) (6=9) (2=9) , gen (simclass_parent)
recode simclass_parent (4=2) (7=3) (8=4) (9=5)
label define simclass 1"Old middle class" 2"Production wrkrs" 3"SCP" 4"Service wrkrs" 5"Others"
label values simclass_parent simclass

recode class8_r (3=1) (5=1) (6=9) (2=9) , gen (simclass_r)
recode simclass_r (4=2) (7=3) (8=4) (9=5)
label values simclass_r simclass

*Different order of variables
capture drop simclass_r2
recode simclass_r simclass_parent (1=4) (2=2) (3=3) (4=1), gen(simclass_r2 simclass_parent2)
label define simclass_r2 1"Service workers" 2"Production workers" 3 "SCP" 4"OMC" 5"Others"
label values simclass_r2 simclass_parent2 simclass_r2
tab1 simclass_r2 simclass_r 
tab1 simclass_parent2 simclass_parent

************************
*rrecoding of transitions

gen trans=.
recode trans (.=1) if simclass_parent==2 & simclass_r==2 // * Immobile prod workers
recode trans (.=2) if simclass_parent==4 & simclass_r==4 //   Immobile service workers
recode trans (.=3) if simclass_parent==2 & simclass_r==4 // * Prod wkers - Serv wkers
recode trans (.=4) if simclass_parent==4 & simclass_r==2 //   Serv wkers - Prod wkers
recode trans (.=5) if simclass_parent==3 & simclass_r==3 // * Immobile SCP

recode trans (.=6) if simclass_parent==2 & simclass_r==3 // * Prod wkers - SCP
recode trans (.=7) if simclass_parent==4 & simclass_r==3 //   Serv wkers - SCP
recode trans (.=8) if simclass_parent==3 & simclass_r==2 //   SCP - worker
recode trans (.=8) if simclass_parent==3 & simclass_r==4 //   SCP - worker

recode trans (.=9) if simclass_parent==2 & simclass_r==1 // * Prod wkers - OMC
recode trans (.=10) if simclass_parent==4 & simclass_r==1 //  Serv wkers - OMC
recode trans (.=11) if simclass_parent==3 & simclass_r==1 // * SCP - OMC

recode trans (.=12) if simclass_parent==1 & simclass_r==2 //  OMC - workers
recode trans (.=12) if simclass_parent==1 & simclass_r==4 //  OMC - workers
recode trans (.=13) if simclass_parent==1 & simclass_r==3 // * OMC - SCP
recode trans (.=14) if simclass_parent==1 & simclass_r==1 // * Immobile OMC

recode trans (.=20) if simclass_parent==1 & simclass_r==5
recode trans (.=21) if simclass_parent==2 & simclass_r==5
recode trans (.=22) if simclass_parent==3 & simclass_r==5
recode trans (.=23) if simclass_parent==4 & simclass_r==5
recode trans (.=24) if simclass_parent==5 & simclass_r==1
recode trans (.=25) if simclass_parent==5 & simclass_r==2
recode trans (.=26) if simclass_parent==5 & simclass_r==3
recode trans (.=27) if simclass_parent==5 & simclass_r==4
recode trans (.=28) if simclass_parent==5 & simclass_r==5

label define trans ///
1 "Immobile prod workers" ///
2 "Immobile serv workers" ///
3 "Prod wkers - Serv wkers" ///
4 "Serv wkers - Prod wkers" ///
5 "Immobile SCP" ///
6 "Prod wkers - SCP" ///
7 "Serv wkers - SCP" ///
8 "SCP - Workers" ///
9 "Prod wkers - OMC" ///
10 "Serv wkers - OMC" ///
11 "SCP - OMC" ///
12 "OMC - Workers" ///
13 "OMC - SCP" ///
14 "Immobile OMC"
label values trans trans
tab trans


*basic variable recoding

recode _all (-1=.a)(-2=.b)(-3=.c)(-4=.d)(-5=.e)(-6=.f)(-7=.g)(-8=.h) 
/* 
-1 	no answer / don't know
-2 	does not apply
-3 	implausible value
-4 	Inadmissible multiple response
-5 	Not included in this version of the questionnaire
-6 	Version of questionnaire with modified filtering
-8 	Question not part of the survey program this year
*/ 

*gen age variable
gen age=(syear-gebjahr)
tab age

*gender
gen female=0
replace female=1 if sex==2
tab female

*recode civil status 
recode pgfamstd (3=3 "other") (1=1 "married/partnership") (2=2 "divorced/separated") (4=2) (5=3) (6=1) (7=3) (8=3), gen(civsta_3)
tab civsta_3

*full-/part-time
tab plb0022_h
recode plb0022_h (1=0 "full-time") (2=1 "part-time") (else=.), gen(parttime)
tab parttime

*level of education
tab pgisced97
recode pgisced97 (0=0 "still in school") (1/2=1 "low") (3/4=2 "medium") (5/6=3 "high"), gen(educat_3)
tab1 pgisced97 educat_3

*party leaning/identification
label define PID 1 "yes" 2 "no" 3 "no answer"
label values plh0011_h PID
tab plh0011_h
tab plh0012_h

*SPD identification/preference
recode plh0012_h (1=1 "SPD") (2/31=0 "other PID"), gen(SPD)
replace SPD=. if plh0011_h==2 | plh0011_h==3
tab SPD

*Left party identification/preference excl. Greens
recode plh0012_h (1=1 "SPD/Die Linke") (2/5=0 "other PID") (6=1 "SPD/Die Linke") (7/31=0 "other PID"), gen(left)
replace left=. if plh0011_h==2 | plh0011_h==3
tab left

*Left party identification/preference incl. Greens
recode plh0012_h (1=1 "SPD/Die Linke/Greens") (2/4=0 "other PID") (5=1 "SPD/Die Linke/Greens") /// 
(6=1 "SPD/Die Linke/Greens") (7/31=0 "other PID"), gen(left_green)
replace left_green=. if plh0011_h==2 | plh0011_h==3
tab left_green


*nationality
tab pgnation
recode pgnation (1=1 "German") (else=0 "other"), gen(german)
tab german

*East/West Germany
tab loc1989 

* Generations of respondents

tab gebjahr
recode gebjahr (1882/1927=1) (1928/1945=2) (1946/1964=3) (1965/1980=4) (1981/1996=5) (1997/2012=6) ///
 , gen(generations)
 
label define generations ///
1 "1927 or earlier" ///
2 "Silent 1928-1945" ///
3 "Boomers 1946-1964" ///
4 "Gen X 1965-1980" ///
5 "Millennials 1981-1996" ///
6 "Gen Z 1997-2012"
label values generations generations
tab generations

recode gebjahr (1882/1927=.) (1928/1945=.) (1946/1964=3) (1965/1980=4) (1981/1996=5) (1997/2012=.), gen(generations2)
label define generations2 ///
3 "Boomers 1946-1964" ///
4 "Gen X 1965-1980" ///
5 "Millennials 1981-1996"
label values generations2 generations2
tab generations2


*income - below / above median
summarize plc0013_h, detail // gross personal income (last month)
recode plc0013_h (min/1841=0 "(below) median")(1842/max=1 "above median"), gen(income_p)
tab income_p
summarize income_p, detail

*income - log of gross yearly income
gen lnincome=ln(plc0013_h*12)
summarize lnincome, detail 



*EGP class of respondents
tab pgegp88 
recode pgegp88 (1=1 "higher service") (2=2 "lower service") (3 4 =3 "routine non-manual") (5 6 11 =5 "small self-empl & farmers") (8 9 10= 4 "working class"), gen(egp_r)
tab egp_r

*EGP class of parents
tab1 fegp88 megp88

recode fegp88 (1=1 "higher service") (2=2 "lower service") (3 4 =3 "routine non-manual") (5 6 11 =5 "small self-empl & farmers") (8 9 10= 4 "working class"), gen(egp_f)

recode megp88 (1=1 "higher service") (2=2 "lower service") (3 4 =3 "routine non-manual") (5 6 11 =5 "small self-empl & farmers") (8 9 10= 4 "working class"), gen(egp_m)

gen egp_parent = egp_f
replace egp_parent = egp_m if egp_f==.a | egp_f==.b
label values egp_parent egp_f
tab egp_parent


numlabel, add


save SOEP_v35_workfile
